package com.itheima.utils;

import com.alibaba.fastjson.JSON;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;

/**
 * @Description: 全局异常处理器
 * @Create by: JJ菜菜
 * @Date: 2021/11/1 20:54
 */

@RestControllerAdvice
public class GloableExceptionHandler {

    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public R<String> myAddUsernameException(SQLIntegrityConstraintViolationException e){
    // Duplicate entry 'admin' for key 'idx_username'
        e.printStackTrace();
        String message = e.getMessage();
        String[] s = message.split(" ");
        String jsonString = JSON.toJSONString(s[2]+"账户已经存在！");
        return R.error(jsonString);
    }

    @ExceptionHandler(Exception.class)
    public R<String> SystemException(Exception e){
        e.printStackTrace();
        return R.error(JSON.toJSONString("服务器忙碌，请稍后再试"));
    }
}
